package customerEnter.Frame.conntroller;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.aspectj.lang.ProceedingJoinPoint;

import customerEnter.Frame.Utils.Log4j;
import customerEnter.tool.WhetherToEmpty;

/**
 * aop的助手類
 * 
 * 类名称：BaseConntrollerFrameHelp 类描述： 创建人：dy 创建时间：2016-8-31 上午11:28:26
 * 
 */
public class BaseConntrollerFrameHelp {
	/**
	 * 輸出日誌信息
	 */
	protected static void outLog4jInfo(ProceedingJoinPoint joinpoint) {
		Log4j.setLog4j(joinpoint.getThis().getClass());
		Log4j.Log4jOutInfo("類名：" + joinpoint.getTarget().getClass().getName());
		Log4j.Log4jOutInfo("方法名：" + joinpoint.getSignature().getName());
	}

	/**
	 * 運行當前進入aop的方法
	 */
	protected static void runMainMethod(ProceedingJoinPoint joinpoint) throws Throwable {
		joinpoint.proceed();
	}

	/**
	 * 查看ConntroJsonMap有沒有數據 有就用out put發送
	 * 
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	protected static void CalculationMapSen(ProceedingJoinPoint joinpoint) throws IllegalAccessException,
			IllegalArgumentException, InvocationTargetException {
		int inobj = 0;
		Object[] objects = null;

		Method[] methods = joinpoint.getThis().getClass().getDeclaredMethods();
		Map<String, Method> mapMethod = new HashMap<String, Method>();

		for (Method method : methods) {
			mapMethod.put(method.getName(), method);
		}

		inobj = ifSend(mapMethod, joinpoint, objects, "getConntroJsonObject", "getConntroJsonMap");
		sendOutPrintData(mapMethod.get("sendOutPrint"), joinpoint, inobj);
	}

	public static int ifSend(Map<String, Method> mapMethod, ProceedingJoinPoint joinpoint, Object[] objects,
			String... is) {
		int i = 0;
		for (String string : is) {
			i = IfConntroJsonMapSize(MethodInvoke(mapMethod.get(string), joinpoint, objects));
			if (i == 1) {
				break;
			}
		}
		return i;
	}

	// 判斷要不要發送數據
	public static Object MethodInvoke(Method method, ProceedingJoinPoint joinpoint, Object... objects) {
		Object object = null;
		try {
			object = method.invoke(joinpoint.getThis(), objects);
		} catch (IllegalAccessException e) {

			Log4j.Log4JOutError(e.getMessage());
		} catch (IllegalArgumentException e) {

			Log4j.Log4JOutError(e.getMessage());
		} catch (InvocationTargetException e) {

			Log4j.Log4JOutError(e.getMessage());
		}
		return object;
	}

	/**
	 * 判斷有沒有值
	 */
	public static int IfConntroJsonMapSize(Object object) {
		if (!WhetherToEmpty.ObjectWhetherToEmpty(object)) {
			return 1;
		} else {
			return 0;
		}
	}

	/**
	 * 發送數據到頁面
	 */
	public static void sendOutPrintData(Method method, ProceedingJoinPoint joinpoint, int... is) {
		Object[] objects = null;
		for (int i : is) {
			if (i == 1) {
				MethodInvoke(method, joinpoint, objects);
				break;
			}
		}
		Log4j.Log4jOutInfo("啟動 sendOutPrint  發送數據");
	}

}
